<template>
  <div class="list">
    <div class="item" v-for="goods in goodsList" :key="goods.id">
      <div class="item-l"><img :src="goods.src" alt=""></div>
    

    <div class="item-r">
      <div class="item-title">{{goods.title}}</div>
      <div class="item-price">{{goods.price | currency}}</div>
      <div class="item-option">
        <button @click="add(goods)">加入购物车</button>
      </div>
    </div>
    </div>
  </div>
</template>

<script>
// 引入mapState,mapActions,可以快速使用store的数据方法
import {mapState, mapActions} from 'vuex'
export default {
  methods: {
    ...mapActions('shopcart', ['add'])
  },
  computed: {
    // ...在es6中是对象结构
    ...mapState({
      goodsList:state => state.goodslist.list
    })
  },
  // 组件创建之后，调用getList方法，对list进行赋值
  created() {
    this.$store.dispatch('goodslist/getList')
  },
  filters: {
    currency(value) {
      return ' ¥ ' + value;
    }
  }
}
</script>

<style scoped>
  .item {
    width: 100vw;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px dashed #eee;
  }
  .item-l {
    flex: 1;
  }
  .item-l > img {
    margin-left: 10px;
  }
  .item-r {
    flex: 1;
  }
  .item-title {
    font-weight: 800;
  }
  .item-price {
    margin-top: 10px;
    color: red;
  }
  .item-option > button {
    background-color: red;
    color: white;
    border-radius: 5px;
    border: 0;
    margin-top: 10px;
  }
</style>